import CryptoJS from "crypto-js";

let keyStr = "ATityu#UT98!24eX";
let ivStr = "ATityu#UT98!24eX";

export function encryptAES(txt) {
  if(txt){
    let key = CryptoJS.enc.Utf8.parse(keyStr);
    let iv = CryptoJS.enc.Utf8.parse(ivStr);
    let sics = CryptoJS.enc.Utf8.parse(txt);

    let encrypted = CryptoJS.AES.encrypt(sics, key, {
      iv,
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  }else{
    return txt
  }


}
// AES 解密方法
export function decryptAES(txt) {

  try {
    if(txt){
      let key = CryptoJS.enc.Utf8.parse(keyStr);
      let decrypt = CryptoJS.AES.decrypt(txt, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
      return CryptoJS.enc.Utf8.stringify(decrypt).toString();
    }else{
      return "";
    }
  }catch (e) {
    return txt
  }


}


export function phoneNumberDesc(phone){
  if (phone) {
    return  phone.replace(/^(.{3})(?:\w+)(.{3})$/, "\$1***\$2");
  }else{
    return ''
  }
}

export function namePrivate(name) {
  if (name) {
    if (name.length === 2) {
      return name.substring(0, 1) + '*' // 截取name的第一个字符，第二个字符变成*
    } else if (name.length === 3) {
      return name.substring(0, 1) + '*' + name.substring(2, 3) // 截取name的第一个和第三个字符，第二个字符变成*
    } else if (name.length > 3) {
      return name.substring(0, 1) + '*' + '*' + name.substring(3, name.length);
    }else{
      return name
    }
  } else {
    return ''
  }
}

export function emailPrivate(email) {

  if (email && email.indexOf("@")>0) {
    let str = email.split("@");
    const str1 =
      str[0].substr(0, str[0].length - 2) + "*****" + str[1].substr(2);
    return str1
  } else {
    return ''
  }
}
